/*
 * Copyright (C) 2012 Virglinio & Asociados
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.virglinio.telemedicina.rehabilitacion.openGL;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;


import android.content.Context;
import android.opengl.GLU;
import android.opengl.GLSurfaceView.Renderer;

/**
 * @author mario
 *
 */
public class GLRenderer implements Renderer {
	
	private Rectangulo rectanguloBrazo;
	private Rectangulo rectanguloAntebrazo;

	private float anguloRectangulo = 180;
	private Rectangulo rectanguloTorso;
	private float xSpeedRectanguloTorso = 0.1f;
	private float yRotRectanguloTorso = 0;
	private float xRotRectanguloTorso = 0;
	private float ySpeedRectanguloTorso = 0.1f;
	
	public GLRenderer(Context context){
		rectanguloBrazo = new Rectangulo();
		rectanguloAntebrazo = new Rectangulo();
		rectanguloTorso = new Rectangulo();
	}

	/**
	 * 
	 */
	public void onSurfaceCreated(GL10 arg0, EGLConfig arg1) {
		// TODO Auto-generated method stub
		arg0.glClearColor(1.0f, 1.0f, 1.0f, 1.0f);	// Set color's clear-value to black
		arg0.glClearDepthf(1.0f);            		// Set depth's clear-value to farthest
		arg0.glEnable(GL10.GL_DEPTH_TEST);   		// Enables depth-buffer for hidden surface removal
		arg0.glDepthFunc(GL10.GL_LEQUAL);    		// The type of depth testing to do
		arg0.glHint(GL10.GL_PERSPECTIVE_CORRECTION_HINT, GL10.GL_NICEST);  // nice perspective view
		arg0.glShadeModel(GL10.GL_SMOOTH);   		// Enable smooth shading of color
		arg0.glDisable(GL10.GL_DITHER);      		// Disable dithering for better performance		
	}
	
	public void onDrawFrame(GL10 gl) {
		// TODO Auto-generated method stub
		gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
		
		gl.glLoadIdentity();                				// Reset the mode-view matrix
		gl.glTranslatef(0.0f, 1.0f, -6.0f);  				// Translate right and into the screen
		//gl.glScalef(0.5f, 0.5f, 0.5f);      				// Scale down
		gl.glRotatef(90.0f, 0.0f, 1.0f, 0.0f); 	// rotate about the axis (1,1,1)
		rectanguloBrazo.draw(gl);	
		
		gl.glLoadIdentity();                				// Reset the mode-view matrix
		gl.glTranslatef(4.0f, 0.0f, -6.0f);  				// Translate right and into the screen
		//gl.glScalef(0.5f, 0.5f, 0.5f);      				// Scale down
		
		gl.glRotatef(0.0f, 1.0f, 0.0f, 0.0f); 	// rotacíon alrededor del eje x
		gl.glRotatef(-46.0f, 0.0f, 1.0f, 0.0f); 	// rotacíon alrededor del eje y
		gl.glRotatef(-34.0f, 0.0f, 0.0f, 1.0f); 	// rotacíon alrededor del eje z
		rectanguloAntebrazo.draw(gl);
		
		gl.glLoadIdentity();
		gl.glTranslatef(-3.0f, 0.0f, -6.0f);
		gl.glRotatef(yRotRectanguloTorso, 0.0f, 1.0f, 0.0f);	// rotación alrededor del eje "y"
		gl.glRotatef(90, 1.0f, 0.0f, 0.0f);						// rotación 90º alrededor del eje "x"
		rectanguloTorso.draw(gl);
		
		//xRotRectanguloTorso += xSpeedRectanguloTorso;
		//yRotRectanguloTorso += ySpeedRectanguloTorso;
	}

	public void onSurfaceChanged(GL10 gl, int with, int height) {
		// TODO Auto-generated method stub
		if (height == 0) {
			height = 1;   // To prevent divide by zero
		}
		float aspect = (float)with / height;

		// Set the viewport (display area) to cover the entire window
		gl.glViewport(0, 0, with, height);

		// Setup perspective projection, with aspect ratio matches viewport
		gl.glMatrixMode(GL10.GL_PROJECTION); 	// Select projection matrix
		gl.glLoadIdentity();                 	// Reset projection matrix
		// Use perspective projection
		GLU.gluPerspective(gl, 80, aspect, 0.1f, 100.0f);

		gl.glMatrixMode(GL10.GL_MODELVIEW);  // Select model-view matrix
		gl.glLoadIdentity();                 // Reset			
	}
	
	
}
